<?php
$cityH = & xoops_getmodulehandler('city','neogeo');
$regionH = & xoops_getmodulehandler('region','neogeo');
$countryH = & xoops_getmodulehandler('country','neogeo');
$itemH = & xoops_getmodulehandler('item');
include_once XOOPS_ROOT_PATH."/class/template.php";
	$XoopsTpl = new XoopsTpl();

function neoobject_admin_item_main(){
	global $eh,$cityH,$regionH,$countryH,$XoopsTpl;
	
#	$mimetypes = include_once(XOOPS_ROOT_PATH."/class/mimetypes.inc.php");

	$itempropH = & xoops_getmodulehandler('itemprop');
	$itempropcatH = & xoops_getmodulehandler('itempropcat');	
	$itemH = & xoops_getmodulehandler('item');
	$idx_cityH = & xoops_getmodulehandler('idx_city','neogeo');
	$idx_cityH->indexing();

	$itemO = $itemH->create();	
	$itemO->idx_cities = $idx_cityH->getObjects();



xoops_cp_header();
	echo $XoopsTpl->fetch('db:neoobject_admin_mainmenu.html');
    
	
	$allpropcategories = $itempropcatH->getObjects(null,1);	
	foreach ($allpropcategories as $itempropcatO){
		  $itempropcatO->properties =	 $itempropH->getObjects(new Criteria("propcat",$itempropcatO->getVar("id")),1);
		  $itemO->allpropcategories[] = $itempropcatO;

	}

	$XoopsTpl->assign_by_ref ("items",  $itemH->getObjects());
	$XoopsTpl->assign_by_ref('itemO',$itemO);
	
	echo $XoopsTpl->fetch('db:neoobject_admin_item_main.html');

	
	xoops_cp_footer();
}
function neoobject_admin_item_create_ok(){
	global $eh,$cityH,$regionH,$countryH,$itemH;
	#print_r($_FILES);


		$itempropH = & xoops_getmodulehandler('itemprop');
		$itemproparr = $itempropH->getObjects(null,true);

		$itemO = & $itemH->create();
		$itemO->setFormVars($_POST,'item_');						
		$itemH->insert($itemO);
		 $_SESSION['neoobject']['item']['city_id'] = $itemO->getVar('city_id');
		 $_SESSION['neoobject']['item']['type_id'] = $itemO->getVar('type_id');
		if ($_POST['itemprop2item_value'] && is_array($_POST['itemprop2item_value'])) {
			
			$prop2itemH = & xoops_getmodulehandler('itemprop2item');
		foreach ($_POST['itemprop2item_value'] as $pkey=>$pvalue){
			unset($prop2itemO);
			$prop2itemO = $prop2itemH->create();
			$prop2itemO->setVar("propid",$pkey);
			$prop2itemO->setVar("value",$pvalue);
			$prop2itemO->setVar("itemid",$itemO->getVar("id"));	
			$prop2itemH->insert($prop2itemO)		;
			
			
		}
		
		}
		neoobject_admin_imagephotos_upload($itemO);
		redirect_header("index.php?type=item");
		exit();
}


function neoobject_admin_item_edit(){
	global $eh,$cityH,$regionH,$countryH,$itemH,$XoopsTpl;
	$xoopsimageH =& xoops_gethandler('image');
	$xoopsimage2itemH = & xoops_getmodulehandler("xoopsimage2item");
	$itempropH = & xoops_getmodulehandler('itemprop');
	$itemprop2itemH = &  xoops_getmodulehandler('itemprop2item');
	$itempropcatH = & xoops_getmodulehandler('itempropcat');	
	$itemO  = $itemH->get($_GET['id']);
	$idx_cityH = & xoops_getmodulehandler('idx_city','neogeo');
	$idx_cityH->indexing();
	$itemO->idx_cities = $idx_cityH->getObjects();
	xoops_cp_header();
	
	echo $XoopsTpl->fetch('db:neoobject_admin_mainmenu.html');
	
	
	$allpropcategories = $itempropcatH->getObjects(null,1);	
	#$itemO->itemprop2itemH = 
	foreach ($allpropcategories as $itempropcatO){
		  $properties =	 $itempropH->getObjects(new Criteria("propcat",$itempropcatO->getVar("id")),1);
			$itempropcatO->properties = array();
		  foreach ($properties as $itempropO){
			$criteria = new CriteriaCompo(new Criteria("itemid",$itemO->getVar("id")));
			$criteria->add(new Criteria("propid",$itempropO->getVar("id")));
		   $itemprop2itemarr = $itemprop2itemH->getObjects($criteria);

		   	if (is_object($itemprop2itemarr[0])) {
#				print_r($itemprop2itemarr[0]); 
		   		$itempropO->value=$itemprop2itemarr[0]->getVar("value");
				
			};
		    $itempropcatO->properties[]  = $itempropO;	  
		  }
		  $itemO->allpropcategories[] = $itempropcatO;

	}

#	print_r($xoopsimage2item);
	#$	
	$xoopsimage2itemarr = $xoopsimage2itemH->getObjects(new Criteria("item_id",$itemO->getVar("id")));
#	print_r($xoopsimage2itemarr);
	$XoopsTpl->assign_by_ref("xoopsimage2itemarr",$xoopsimage2itemarr);
	 	
	$XoopsTpl->assign_by_ref("itemO",$itemO);
	$XoopsTpl->assign_by_ref("xoopsimageH",$xoopsimageH);

echo $XoopsTpl->fetch('db:neoobject_admin_item_edit.html');
	xoops_cp_footer();

	
}
function neoobject_admin_item_edit_ok(){
	$xoopsimage2itemH = & xoops_getmodulehandler("xoopsimage2item");
	$prop2itemH = & xoops_getmodulehandler('itemprop2item');
	$propH = & xoops_getmodulehandler('itemprop');
	$itemH = & xoops_getmodulehandler('item');
	$itemO  = $itemH->get($_GET['id']);
	$itemO->setFormVars($_POST,'item_');	
	$itemH->insert($itemO);
		 $_SESSION['neoobject']['item']['city_id'] = $itemO->getVar('city_id');
		 $_SESSION['neoobject']['item']['type_id'] = $itemO->getVar('type_id');
 	$prop2itemarr = $prop2itemH->getObjects(new Criteria("itemid",$itemO->getVar("id")));
	foreach ($prop2itemarr as $prop2itemO){
		$prop2itemH->delete($prop2itemO);
	}

	if ($_POST['itemprop2item_value'] && is_array($_POST['itemprop2item_value'])) {
			
			$prop2itemH = & xoops_getmodulehandler('itemprop2item');
		foreach ($_POST['itemprop2item_value'] as $pkey=>$pvalue){
			unset($prop2itemO);
			$prop2itemO = $prop2itemH->create();
			$prop2itemO->setVar("propid",$pkey);
			$prop2itemO->setVar("value",$pvalue);
			$prop2itemO->setVar("itemid",$itemO->getVar("id"));	
			$prop2itemH->insert($prop2itemO)		;
			
			
		}
		
	}
#	print_r($_POST['deleteitems']);
	if (is_array($_POST['deleteitems']))	 {
		foreach ($_POST['deleteitems'] as $xoopsimage2item_id){
			
			$xoopsimage2itemO = $xoopsimage2itemH->get($xoopsimage2item_id);
#			print_R($xoopsimage2itemO);
			$xoopsimage2itemH->delete($xoopsimage2itemO);
			
			
		}
	}
	include_once XOOPS_ROOT_PATH."/modules/neosystem/include/functions.php";
		neosystem_admin_imagephotos_upload($itemO,"item",'neoobject');
		redirect_header("index.php?type=item");
		exit();

}
function neoobject_admin_item_delete_ok(){
	$prop2itemH = & xoops_getmodulehandler('itemprop2item');
	$propH = & xoops_getmodulehandler('itemprop');
	$itemH = & xoops_getmodulehandler('item');
	$itemO  = $itemH->get($_GET['id']);
	$itemO->setFormVars($_POST,'item_');	
	$itemH->delete($itemO);

 	$prop2itemarr = $prop2itemH->getObjects(new Criteria("itemid",$itemO->getVar("id")));
	foreach ($prop2itemarr as $prop2itemO){
		$prop2itemH->delete($prop2itemO);
	}
		redirect_header("index.php?type=item");
		exit();

}


function neoobject_admin_imagephotos_upload(&$item){
	  include_once XOOPS_ROOT_PATH.'/class/uploader.php';
			$image_handler =& xoops_gethandler('image');

$xoopsimagecategoryH = & xoops_gethandler("imagecategory");
  $imagecatH = & xoops_getmodulehandler("imagecat");
  $imagecats = $imagecatH->getObjects(null,true);

$uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png'),200000);
	$uploader->setPrefix("tmp");
	for ($i=0 ;$i<count($_FILES['item_photo']['name']);$i++) 
    if ($uploader->fetchMedia('item_photo',$i)) {
        if (!$uploader->upload()) {
            $err = $uploader->getErrors();
        } else {

        		if (preg_match("/\.([a-zA-Z0-9]+)$/", $uploader->getSavedFileName(), $matched))  {$ext = $matched[1];}

			$filename_base = 'neoobject_item'.$item->getVar("id")."_".uniqid("").'.'.$ext;
         foreach ($imagecats as $imagecatO){

			
		 	$prefix = $imagecatO->getVar("prefix");
#			echo $prefix;
			
			$filename = $prefix.$filename_base;
			copy(XOOPS_UPLOAD_PATH."/".$uploader->getSavedFileName(),XOOPS_UPLOAD_PATH."/".$filename);

			
#		    $uploader->setPrefix($prefix);
		     #$uploader->targetFileName = uniqid($prefix);
		    $xoopsimgcat = $xoopsimagecategoryH->get($imagecatO->getVar("xoopsimagecat"));
            $image =& $image_handler->create();
            $image->setVar('image_name', $filename);
            $image->setVar('image_nicename', $prefix.$uploader->mediaName);
            $image->setVar('image_mimetype', $uploader->getMediaType());
            $image->setVar('image_created', time());
            $image->setVar('image_display', 1);
            $image->setVar('image_weight', 0);
            $image->setVar('imgcat_id', $xoopsimgcat->getVar("imgcat_id"));
            if ($xoopsimgcat->getVar('imgcat_storetype') == 'db') {
                $fp = @fopen($uploader->getSavedDestination(), 'rb');
                $fbinary = @fread($fp, filesize($uploader->getSavedDestination()));
                @fclose($fp);
                $image->setVar('image_body', addslashes($fbinary));
                @unlink($uploader->getSavedDestination());
            }
            if (!$image_handler->insert($image)) {
                $err = sprintf(_FAILSAVEIMG, $image->getVar('image_nicename'));
            }
			if (!($imagecatO->getVar("prefix"))) {
				#print_r($item);
#				echo "create xoopsimage2item";
			$xoopsimage2itemH = & xoops_getmodulehandler('xoopsimage2item');
			$xoopsimage2itemO = $xoopsimage2itemH->create();
#				echo "item_id-".$item->getVar("id");
			$xoopsimage2itemO->setVar("item_id",$item->getVar("id"));
			$xoopsimage2itemO->setVar("xoopsimage_id",$image->getVar("image_id"));			
			$xoopsimage2itemH->insert($xoopsimage2itemO);
			}
		 } //foreach	
 			unlink(XOOPS_UPLOAD_PATH."/".$uploader->getSavedFileName());
		}
    } else {
        $err = sprintf(_FAILFETCHIMG, 0);
        $err .= '<br />'.implode('<br />', $uploader->getErrors(false));
    }
#} // for
		echo $err;
}

?>
